package cn.aifeix.org.service.impl;

import cn.aifeix.basic.service.impl.BaseServiceImpl;
import cn.aifeix.basic.util.MD5Utils;
import cn.aifeix.basic.util.StrUtils;
import cn.aifeix.org.domain.Employee;
import cn.aifeix.org.mapper.EmployeeMapper;
import cn.aifeix.org.service.EmployeeService;
import cn.aifeix.user.domain.Logininfo;
import cn.aifeix.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
     * 通过部门id查询员工信息
     * @param departmentId
     * @return
     */
    @Override
    public List<Employee> findByDeptId(Long departmentId) {
        return employeeMapper.findByDeptId(departmentId);
    }

    @Override
    public Employee findByLogininfoId(Long id) {
        return employeeMapper.findByLogininfoId(id);
    }

    /**
     * 通过店铺id查询店铺员工
     * @param shopId
     * @return
     */
    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }

    /**
     * 初始化员工表属性 盐值及密码
     * @param employee
     */
    private void initEmployee(Employee employee){
        // 设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        // 根据盐值及密码使用MD5加密后设置
        String md5Pwd = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(md5Pwd);
    }

    /**
     * 将employee对象数据拷贝到logininfo中，并返回对象
     * @param employee
     */
    private Logininfo employeeConvertLogininfo(Employee employee){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        // 设置用户类型为员工
        logininfo.setType(1);
        return logininfo;
    }

    @Override
    public void add(Employee employee) {
        // 先将盐值及加密后的密码赋给employee
        initEmployee(employee);
        // 将employee数据赋给logininfo
        Logininfo logininfo = employeeConvertLogininfo(employee);
        // 保存logininfo数据
        logininfoMapper.add(logininfo);
        // 将logininfo的主键id存入employee的logininfo_id中
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.add(employee);
        // 员工与角色中间表的添加
        employeeMapper.addMiddle(employee.getId(),employee.getRoles());
    }

    @Override
    public void update(Employee employee) {
        // 重新获取盐值，将密码加密
        String salt = StrUtils.getComplexRandomString(32);
        String encrypByMd5 = MD5Utils.encrypByMd5(salt);
        // 设置employee及logininfo表
        employee.setSalt(salt);
        employee.setPassword(encrypByMd5);
        // 将employee数据赋给logininfo
        Logininfo logininfo = employeeConvertLogininfo(employee);
        // 修改需要id，所以我们要获取emp的logininfoid，然后给我们的新的logininfo对象设置好
        if (null == employee.getLogininfoId()) {
            // 如果获取的emp的logininfoid是空的，那么证明没有同步到Logininfo中，所以我们需要同步一下
            logininfoMapper.add(logininfo);
            // 再给我们的employee同步logininfoid
            employee.setLogininfoId(logininfo.getId());
            // 再修改
            employeeMapper.update(employee);
        } else {
            logininfo.setId(employee.getLogininfoId());
            // 先修改logininfo
            logininfoMapper.update(logininfo);
            // 再修改employee
            employeeMapper.update(employee);
        }
        // 员工与角色中间表的修改
        // 先删除中间表
        employeeMapper.deleteMiddle(employee.getId());
        // 再添加中间表
        employeeMapper.addMiddle(employee.getId(),employee.getRoles());
    }

    @Override
    public void delete(Long id) {
        // 通过id查找员工
        Employee byId = employeeMapper.findById(id);
        // 获取员工logininfoId
        Long logininfoId = byId.getLogininfoId();
        if (null == logininfoId){
            // 为空则两表无关联，直接删除employee表
            employeeMapper.delete(logininfoId);
        }else {
            // 删除两张表
            logininfoMapper.delete(logininfoId);
            employeeMapper.delete(id);
        }


    }
}
